from sqlite3 import connect
import share
import config
'''this module is used for registering user who use service'''

TAG = 'REG'

class Register(object):
	def __init__(self):
		self.__conn = connect(config.DATABASE)
		self.__cur = self.__conn.cursor()
	def __del__(self):
		self.__cur.close()
	def register_id(self, fetion_id, passwd):
		if self.__check(fetion_id, passwd):
			return
		cmd = 'select %s from %s' %(config.fetion_id, config.REGINFO_TBL)
		datacur = self.__cur.execute(cmd)
		ids = datacur.fetchall()
		for id in ids:
			if id[0] == fetion_id:
				print TAG, 'fetionid alreay registered'
				return
		share.fetion.add_buddy('fetion', fetion_id)
		cmd = 'insert into %s (%s, %s) values (?, ?)' %(config.REGINFO_TBL, config.FETION_ID, config.PASSWD)
		self.__cur.execute(cmd, (fetion_id, passwd))
		self.__conn.commit()
	def register_mobile(self, mobile, passwd):
		if self.__check(mobile, passwd):
			return
		cmd = 'select %s from %s' %(config.MOBILE, config.REGINFO_TBL)
		dataCur = self.__cur.execute(cmd)
		mobiles = dataCur.fetchall()
		for mob in mobiles:
			if mob[0] == mobile:
				print TAG, 'mobile alreay registered'
				return;
		share.fetion.add_buddy('mobile', mobile)
		cmd = 'insert into %s (%s, %s) values (?, ?)' %(config.REGINFO_TBL, config.MOBILE, config.PASSWD)
		self.__cur.execute(cmd, (mobile, passwd))
		self.__conn.commit()
	def register_email(self, email, passwd):
		pass
	def __check(self, reg_id, passwd):
		if not reg_id or not passwd:
			print TAG, 'id and passwd must be specifed!'
			return -1
		return 0 #no error

if __name__ == '__main__':
	reg = Register()
	#reg.register_id('927041048', '308831015ppp')
	#reg.register_mobile('13601804916', 'sj19840615')
	reg.register_mobile('15900906440', '19830330')
